﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp_core
{
    public class TaskShow
    {
        /// <summary>
        /// 常规task，异步等待
        /// </summary>
        public void Show()
        {
            var func = new Func<string>(() =>
            {
                Console.WriteLine($"子线程func执行：线程id={Thread.CurrentThread.ManagedThreadId}。{DateTime.Now.ToString("HH:mm:ss:fff")}");
                Thread.Sleep(10 * 1000);//改成3或10是有区别的
                return $"子线程func执行结束：线程id={Thread.CurrentThread.ManagedThreadId}。{DateTime.Now.ToString("HH:mm:ss:fff")}";
            });

            var task1 = Task.Run<string>(func);//开新的线程去执行，执行时间10s(理论上)

            //以下是主线程执行，执行时间5秒(理论上)
            foreach (var item in Enumerable.Range(0, 5))
            {
                Thread.Sleep(1 * 1000);
                Console.WriteLine($"主线程执行顺序-{item},线程id={Thread.CurrentThread.ManagedThreadId}。{DateTime.Now.ToString("HH:mm:ss:fff")}");
            }

            //主线线程执行了5秒，现在获取子线程的结果，所以还需要等待5秒。(理论上)
            var result = task1.Result;
            Console.WriteLine(result);
            Console.WriteLine($"所有线程执行结束,线程id={Thread.CurrentThread.ManagedThreadId}。{DateTime.Now.ToString("HH:mm:ss:fff")}");
        }


        #region Show2  :Async/Await


        /// <summary>
        /// Async/Await
        /// </summary>
        public void Show2()
        {
            var task = GetFirstCharactersCountAsync(10);
            var result = task.Result;
            Console.WriteLine($"{result},{DateTime.Now.ToString("HH:mm:ss:fff")}");
        }

        private static readonly HttpClient s_client = new HttpClient();

        /// <summary>
        /// task传递出去，使用异步时，没有等待的逻辑，由调用者，自行等待
        /// </summary>
        /// <returns></returns>
        public Task<string> GetHtmlAsync()
        {
            // Execution is synchronous here
            var uri = new Uri("https://www.dotnetfoundation.org");

            return s_client.GetStringAsync(uri);
        }

        /// <summary>
        /// task传递出去，使用异步时，有等待逻辑，由方法体，设定等待
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public async Task<string> GetFirstCharactersCountAsync(int count)
        {
            // Execution is synchronous here
            var uri = new Uri("https://www.dotnetfoundation.org");

            // Execution of GetFirstCharactersCountAsync() is yielded to the caller here
            // GetStringAsync returns a Task<string>, which is *awaited*
            var page = await s_client.GetStringAsync(uri);

            // Execution resumes when the client.GetStringAsync task completes,
            // becoming synchronous again.

            if (count > page.Length)
            {
                return page;
            }
            else
            {
                return page.Substring(0, count);
            }
        }


        #endregion

    }
}
